package client.comm.baoding.dialog

import android.util.Log
import android.view.View
import android.widget.LinearLayout
import androidx.databinding.Observable
import androidx.databinding.ObservableField
import androidx.lifecycle.MutableLiveData
import client.comm.commlib.base.BaseActivity
import client.comm.baoding.R
import client.comm.baoding.api.bean.GoodsDetail
import client.comm.baoding.api.bean.GoodsGylDetail
import client.comm.baoding.databinding.DialogSelectskuBinding
import client.comm.baoding.utils.NavigationBar1.getBottomNavigatorHeight
import client.comm.baoding.utils.StatusBarUtil4
import client.comm.commlib.comm_ui.bean.SkuObj
import client.comm.commlib.dipToPx
import client.tzstore.oo.dialog.BottomBindingDialog
import java.math.BigDecimal
import kotlin.properties.Delegates

class SelectSkuDialog(val context: BaseActivity<*, *>, val type: String) :
    BottomBindingDialog<DialogSelectskuBinding>(context) {
    val activity: BaseActivity<*, *>


    val selectNum = ObservableField<String>("1").apply {
        addOnPropertyChangedCallback(object : Observable.OnPropertyChangedCallback() {
            override fun onPropertyChanged(sender: Observable?, propertyId: Int) {
                sender?.let {
                    it as ObservableField<String>
                    var num = 0
                    try {
                        if (!it.get().isNullOrEmpty())
                            num = it.get()!!.toInt()
                    } catch (e: Exception) {
                    }

                    binding.reducueTv.isEnabled = num > 1
                }
            }
        })
    }
    var goods: GoodsDetail? = null

    val selectSku by lazy {
        MutableLiveData<SkuObj>()
    }

    init {
        activity = context
    }

    override fun getV(): Int {
        return R.layout.dialog_selectsku
    }

    override fun postBinding() {
        binding.event = this
        window?.setWindowAnimations(R.style.dialogWindowAnimButtomToTop);
        window?.setBackgroundDrawableResource(android.R.color.transparent)

        if (type.isNotEmpty() && type.equals("1")) {
            var layout = binding.btnOk.layoutParams as LinearLayout.LayoutParams
            if (!StatusBarUtil4.isNavBarHide(context)) {
                layout.bottomMargin =
                    getBottomNavigatorHeight(context) + dipToPx(context.resources!!, 20.0f).toInt()
            } else {
                layout.bottomMargin = 0 //将默认的距离底部改为0
            }
            binding.btnOk.layoutParams = layout
        }

        // 条件判断 是否为sku商品
        goods?.let {

            activity.getImgLoader().load(it.goods_img).into(binding.img)
            binding.goodsName = it.name

            if (it.price > 0) {
                binding.tvPrice.text = "￥${it.price}"
            }

        }
    }

    fun changeNum(n: Int) {
        var num = 0
        try {
            num = selectNum.get()!!.toInt()
        } catch (e: Exception) {
        }
        num += n
        if (num < 1)
            num = 1
        selectNum.set("$num")
    }

    var ok: ((uri: String) -> Unit)? = null

    fun queren() {
        cancel()
        ok?.invoke(selectNum.get() as String)
    }
}